package com.langyue.recordmanage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.langyue.recordmanage.enums.ResponseCodeEnum;
import com.langyue.recordmanage.model.query.UserPageQuery;
import com.langyue.recordmanage.model.query.UserQuery;
import com.langyue.recordmanage.pojo.User;
import com.langyue.recordmanage.service.UserService;
import com.langyue.recordmanage.utils.response.ResponseBuilder;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 用户相关
 * @Author: wanglang
 * @CreateTime: 2022/4/1
 * @Company:
 */
@RestController
@RequestMapping("/user")
public class UserController {
    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    // swagger方法注解
    @ApiOperation(value = "通过id查询用户",notes = "通过id查询用户")
    @RequestMapping(value = "/queryById/{id}",method = RequestMethod.GET)
    public Object queryById(@ApiParam(value = "id",required = true) @PathVariable Long id){
        return userService.queryById(id);
    }

    @ApiOperation(value = "分页查询用户",notes = "分页查询用户")
    @RequestMapping(value = "/userIPage",method = RequestMethod.GET)
    public Object userIPage(UserPageQuery query){
        return ResponseBuilder.build(ResponseCodeEnum.SUCCESS,userService.userIPage(query));
    }

    @ApiOperation(value = "添加用户",notes = "添加用户")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object add(@RequestBody UserQuery userQuery){
        try {
            userService.add(userQuery);
            return ResponseBuilder.build(ResponseCodeEnum.SUCCESS,true);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResponseBuilder.build(ResponseCodeEnum.FAIL,false);
    }

    @ApiOperation(value = "更新用户",notes = "更新用户")
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public Object update(@RequestBody UserQuery userQuery){
        try {
            userService.update(userQuery);
            return ResponseBuilder.build(ResponseCodeEnum.SUCCESS,true);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResponseBuilder.build(ResponseCodeEnum.FAIL,false);
    }

    @ApiOperation(value = "通过id删除用户",notes = "通过id删除用户")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public Object delete(@ApiParam(value = "id",required = true) @PathVariable Long id){
        try {
            userService.deleteById(id);
            return ResponseBuilder.build(ResponseCodeEnum.SUCCESS,true);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResponseBuilder.build(ResponseCodeEnum.FAIL,false);
    }
}




























